package com.wn;


import com.wn.service.UserService;
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 可以把 接口的代理对象创建出来 创建出来的对象就是jvm中的对象  （.class）
 *
 *
 */
public class ProxyUtils {

    private static String DEFAULT_CLASS_NAME = "$Proxy";

    public static byte[] saveGenerateProxyClass(String path, Class<?>[] interfaces) {

        byte[] classFile = ProxyGenerator.generateProxyClass(DEFAULT_CLASS_NAME, interfaces);

        FileOutputStream out = null;

        try {
            String filePath = path + "/" + DEFAULT_CLASS_NAME + ".class";
            out = new FileOutputStream(filePath);
            out.write(classFile);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
            } catch (IOException e) {
              
            }
        }
        return classFile;
    }


    public static void main(String[] args) {
        Class<?> interfaces[] = {UserService.class};
        saveGenerateProxyClass("C:/java_lesson", interfaces);
    }

}
